<template>
  <div class="app-container">
    <h2 style="text-align: center;">发布新课程</h2>

    <el-steps :active="2" process-status="wait" align-center style="margin-bottom: 40px;">
      <el-step title="填写课程基本信息"/>
      <el-step title="创建课程大纲"/>
      <el-step title="提交审核"/>
    </el-steps>

    <el-button type="button" @click="dialogChapterFormVisible = true">添加章节</el-button>

    <!--添加和修改章节的弹窗表单-->
    <el-dialog :visible.sync="dialogChapterFormVisible" title="添加章节">
      <el-form :model="chapter" label-width="120px">
        <el-form-item label="章节标题">
          <el-input v-model="chapter.title"/>
        </el-form-item>
        <el-form-item label="章节排序">
          <el-input-number v-model="chapter.sort" :min="0" controls-position="right"/>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogChapterFormVisible = false">取消</el-button>
        <el-button type="primary" @click="saveOrUpdate">确定</el-button>
      </div>
    </el-dialog>

    <!--添加和修改课时的弹窗表单-->
    <el-dialog :visible.sync="dialogVideoFormVisible" title="添加课时">
      <el-form :model="video" label-width="120px">
        <el-form-item label="课时标题">
          <el-input v-model="video.title"/>
        </el-form-item>
        <el-form-item label="课时排序">
          <el-input-number v-model="video.sort" :min="0" controls-position="right"/>
        </el-form-item>
        <el-form-item label="是否免费">
          <el-radio-group v-model="video.isFree">
            <el-radio :label= "0">免费</el-radio>
            <el-radio :label= "1">默认</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="上传视频">
          <!--todo-->
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVideoFormVisible = false">取消</el-button>
        <el-button :disabled="saveVideoBtnDisabled" type="primary" @click="saveOrUpdateVideo">确定</el-button>
      </div>
    </el-dialog>

    <ul class="chanpterList">
      <li
        v-for="chapter in chapterNestedList"
        :key="chapter.id">
        <div>
          {{ chapter.title }}
          <span class="acts">
            <el-button type="button" @click="dialogVideoFormVisible = true; chapterId = chapter.id">添加课时</el-button>
            <el-button type="button" @click="editChapter(chapter.id)">编辑</el-button>
            <el-button type="button" @click="removeChapter(chapter.id)">删除</el-button>
          </span>
        </div>

        <ul class="chanpterList videoList">
          <li
            v-for="video in chapter.children"
            :key="video.id">
            <div>
              {{ video.title }}

              <span class="acts">
                <el-button type="button" @click="editVideo(video.id)">编辑</el-button>
                <el-button type="button" @click="removeVideo(video.id)">删除</el-button>
              </span>
            </div>
          </li>
        </ul>
      </li>
    </ul>

    <el-form label-width="120px">
      <el-form-item>
        <el-button @click="previous">上一步</el-button>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="next">保存并下一步</el-button>
      </el-form-item>
    </el-form>

  </div>

</template>

<script>
import chapter from '@/api/edu/chapter'
import video from '@/api/edu/video'

export default {
  name: 'Chapter',
  data() {
    return {
      saveBtnDisabled: false, // 保存并下一步按钮是否禁用
      courseId: '', // 课程id
      chapterNestedList: [], // 章节list

      // 章节表单用
      dialogChapterFormVisible: false, // 是否显示新增章节用的表单
      chapter: { // 章节对象，用于新增和修改
        title: '',
        sort: 0
      },

      // 课时使用
      saveVideoBtnDisabled: false, // 保存课时按钮是否禁用
      dialogVideoFormVisible: false, // 是否显示新增课时用的表单
      chapterId: '', // 课时所在章节id
      video: { // 课时对象，用于新增和修改
        title: '',
        sort: 0,
        isFree: 0,
        videoSourceId: ''
      }

    }
  },
  created() {
    // 初始化方法
    this.init()
  },
  methods: {
    init() {
      // 从路径获取id
      if (this.$route.params && this.$route.params.id) {
        this.courseId = this.$route.params.id
        // 根据课程id获取章节信息
        this.fetchChapterNestedListByCourseId()
      }
    },

    fetchChapterNestedListByCourseId() {
      // 发起请求
      chapter.getNestedTreeList(this.courseId).then(response => {
        this.chapterNestedList = response.data.items
      })
    },

    // 章节相关方法
    // 添加或修改章节
    saveOrUpdate() {
      this.saveBtnDisabled = true
      // 判断此时的chapter变量是否带有id，如果时新增则不会有id，如果是修改，则回事先调用editChapter方法，为chapter赋id
      if (!this.chapter.id) {
        this.saveData()
      } else {
        this.updateData()
      }
    },

    saveData() {
      this.chapter.courseId = this.courseId
      chapter.save(this.chapter).then(response => {
        this.$message({
          type: 'success',
          message: '保存成功！'
        })
        this.helpSave()
      }).catch((response) => {
        this.$message({
          type: 'error',
          message: response.message
        })
      })
    },

    updateData() {
      chapter.updateById(this.chapter).then(result => {
        this.$message({
          type: 'success',
          message: '修改成功'
        })
        this.helpSave()
      }).catch((result) => {
        this.$message({
          type: 'error',
          message: result.message
        })
      })
    },

    // 保存成功之后执行的方法
    helpSave() {
      this.dialogChapterFormVisible = false // 保存成功后关闭表单窗口
      this.fetchChapterNestedListByCourseId() // 刷新列表
      this.chapter.title = '' // 重置表单章节标题
      this.chapter.sort = 0 // 重置表单章节排序
      this.saveBtnDisabled = false // 解除保存按钮禁用
    },

    // 编辑章节--弹出新增章节的表单，回显当前章节的信息，根据章节id
    editChapter(chapterId) {
      this.dialogChapterFormVisible = true
      chapter.getById(chapterId).then(result => {
        // 将查询到的章节回显
        this.chapter = result.data.item
      })
    },

    // 删除章节
    removeChapter(chapterId) {
      this.$confirm('此操作将永久删除该条记录，是否继续', '提示', {
        cancelButtonText: '取消',
        confirmButtonText: '确定',
        type: 'warning'
      }).then(() => {
        return chapter.removeById(chapterId)
      }).then(() => {
        this.fetchChapterNestedListByCourseId() // 刷新列表
        this.$message({
          type: 'success',
          message: '删除成功'
        })
      }).catch((reason) => {
        if (reason === 'cancel') {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        } else {
          this.$message({
            type: 'error',
            message: reason.message
          })
        }
      })
    },

    // 课时相关方法
    // 添加或修改课时
    saveOrUpdateVideo() {
      this.saveVideoBtnDisabled = true
      // 判断当前的video是否有课时id，如果有就是通过修改按钮点进来的，如果没有就是通过新增按钮点进来的
      if (!this.video.id) {
        this.saveDataVideo()
      } else {
        this.updateDataVideo()
      }
    },

    // 新增课时
    saveDataVideo() {
      this.video.courseId = this.courseId // 绑定课程id
      this.video.chapterId = this.chapterId // 绑定章节id
      video.saveVideoInfo(this.video).then(result => {
        this.$message({
          type: 'success',
          message: '保存成功'
        })
        this.helpSaveVideo()
      }).catch((reason) => {
        this.$message({
          type: 'error',
          message: reason.message
        })
      })
    },

    // 修改课时
    updateDataVideo() {
      video.updateVideoInfoById(this.video).then(result => {
        this.$message({
          type: 'success',
          message: '修改成功'
        })
        this.helpSaveVideo()
      }).catch((reason) => {
        this.$message({
          type: 'error',
          message: reason.message
        })
      })
    },

    helpSaveVideo() {
      this.dialogVideoFormVisible = false // 如果保存成功则关闭弹窗
      this.fetchChapterNestedListByCourseId() // 刷新页面
      this.video.title = '' // 重置
      this.video.sort = 0 // 重置
      this.video.videoSourceId = '' // 重置
      this.saveVideoBtnDisabled = false
    },

    // 修改课时，弹窗回显当前选中课时的基本信息，根据videoId
    editVideo(videoId) {
      this.dialogVideoFormVisible = true
      video.getVideoInfoById(videoId).then(result => {
        this.video = result.data.item
      })
    },

    // 删除课时
    removeVideo(videoId) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        cancelButtonText: '取消',
        confirmButtonText: '确定',
        type: 'warning'
      }).then(() => {
        return video.removeById(videoId)
      }).then(() => {
        this.fetchChapterNestedListByCourseId() // 刷新列表
        this.$message({
          type: 'success',
          message: '删除成功'
        })
      }).catch((reason) => {
        if (reason === 'cancel') {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        } else {
          this.$message({
            type: 'error',
            message: reason.message
          })
        }
      })
    },

    previous() {
      this.$router.push({ path: '/edu/course/info/' + this.courseId })
    },

    next() {
      this.$router.push({ path: '/edu/course/publish/' + this.courseId })
    }
  }
}
</script>

<style scoped>
  .chanpterList{
    position: relative;
    list-style: none;
    margin: 0;
    padding: 0;
  }
  .chanpterList li{
    position: relative;
  }
  .chanpterList div{
    /*float: left;*/
    text-align: left;
    font-size: 20px;
    margin: 10px 0;
    padding: 10px;
    height: 70px;
    line-height: 50px;
    width: 100%;
    /*border: 1px solid #DDD;*/
  }
  .chanpterList .acts {
    float: right;
    font-size: 14px;
  }

  .videoList{
    padding-left: 50px;
  }
  .videoList p{
    float: left;
    font-size: 14px;
    margin: 10px 0;
    padding: 10px;
    height: 50px;
    line-height: 30px;
    width: 100%;
    /*border: 1px dotted #DDD;*/
  }

</style>
